<?php

class location{

	private $conect;

	function __construct(){
		if(array_key_exists('getLocation', $_POST) && $_POST['getLocation'] == 'cities' && array_key_exists('countryID', $_POST)){
			include 'class.database.php';
			$this->getCities($_POST['countryID']);
		}
	}

	public function getCountries(){
		$query = "SELECT * FROM countries";
		$result = mysql_query($query) or die (mysql_error());
		$html = '';
		while ($countries = mysql_fetch_array($result)){
			$html .= '<option value="'.$countries['id'].'">'.$countries['name'].'</option>';
		}
		return $html;
	}
		
	public function getCities($country, $city = 0){
		$result=mysql_query("SELECT * FROM cities WHERE country_id='".(int)$country."'") or die (mysql_error());
		$html = '';
		while ($city=mysql_fetch_array($result)){
			$selected = ($city == $city['id']) ? ' selected="selected"' : '';
			$html .= '<option value="'.$city['id'].'" '.$selected.'>'.$city['name'].'</option>';
		}
		return print json_encode($html);
	}
	
}


$location_obj = new location();